package com.sg.service.biz.calc.nb;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.calc.common.FindTargetDataByIntervalDataDto;
import com.sg.dto.biz.calc.common.SameLevelEvaObjTargetCycleDto;
import com.sg.dto.biz.calc.req.*;
import com.sg.dto.biz.calc.res.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbStandardData {

    @Resource
    private RedisUtil redisUtil;

    /**
     * code:multiColumnToMultiRow
     * name:M4-获取标准数据列转多行（特殊方法）
     * desc:undefined
     * gen by moon at 8/25/2022, 3:05:08 AM
     **/
    @Trace(operationName = "M4-获取标准数据列转多行（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainStandardDataColumnToMultiRowRespDto obtainStandardDataColumnToMultiRow(ObtainStandardDataColumnToMultiRowReqDto reqDto) {
        ObtainStandardDataColumnToMultiRowRespDto retData = new ObtainStandardDataColumnToMultiRowRespDto();
        Field[] declaredFields = StandardDataDto.class.getDeclaredFields();
        Map<String, Field> tarGetValueIndex = new HashMap<>();
        for (Field field : declaredFields) {
            tarGetValueIndex.put(field.getName(), field);
        }
        Map<String, StandardDataDto> arData = new HashMap<>();
        for (StandardDataDto oneItem : reqDto.getStandardDataList()) {
            if (!arData.containsKey(oneItem.getEntityId())) {
                StandardDataDto elm = new StandardDataDto();
                arData.put(oneItem.getEntityId(), elm);
            }
            StandardDataDto elm = arData.get(oneItem.getEntityId());

            String key = CommonFunctionHelper.underlineToHump(oneItem.getDataType());
            Field valueField = tarGetValueIndex.get(key);
            elm.setEntityId(oneItem.getEntityId());
            elm.setFromContentEntityId(oneItem.getFromContentEntityId());


            try {
                if (valueField != null) {
                    valueField.setAccessible(true);
                    if (Objects.equals(valueField.getType(), String.class)) {
                        valueField.set(elm, String.valueOf(oneItem.getDataResult()));
                    } else {
                        valueField.set(elm, oneItem.getDataResult());
                    }

                    Field unitField = tarGetValueIndex.get(key + "Unit");
                    if (unitField != null) {
                        unitField.setAccessible(true);
                        unitField.set(elm, oneItem.getUnit());
                    }

                    //新加逻辑
                    if (oneItem.getDataType().equals("PROGRESS_VALUE")) {
                        elm.setFromLastProgressValue(oneItem.getChangeFromLastValue());
                        elm.setFromLastProgressValueUnit(oneItem.getUnit());
                        elm.setProgressFromLastRatio(oneItem.getChangeFromLastRatio());
                        elm.setProgressFromLastRatioUnit("%");
                    } else if (oneItem.getDataType().equals("ACTUAL_VALUE")) {
                        elm.setChangeFromLastTarget(oneItem.getChangeFromLastValue());
                        elm.setChangeFromLastTargetUnit(oneItem.getUnit());
                        elm.setFromLastTargetLiftRatio(oneItem.getChangeFromLastRatio());
                        elm.setFromLastTargetLiftRatioUnit("%");
                    } else if (oneItem.getDataType().equals("REACH")) {
                        elm.setFromLastReach(oneItem.getChangeFromLastValue());
                        elm.setFromLastReachUnit(oneItem.getUnit());
                        elm.setActualFromLastTimeRatio(oneItem.getChangeFromLastRatio());
                        elm.setActualFromLastTimeRatioUnit("%");
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        retData.setStandardDataList(arData.values().stream().map(p -> BeanUtil.toBean(p, StandardDataDto.class)).collect(Collectors.toList()));

        if (retData.getStandardDataList() != null && retData.getStandardDataList().size() == 1) {
            StandardDataDto firstData = retData.getStandardDataList().get(0);
            BeanUtil.copyProperties(firstData, retData);
        }
        return retData;
    }

    /**
     * code:collectionsAggregation
     * name:M4-数据集聚合（特殊方法）
     * desc:undefined
     * gen by moon at 8/28/2022, 4:08:58 PM
     **/
    @Trace(operationName = "M4-数据集聚合（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public AddCollectionsAggregationRespDto addCollectionsAggregation(AddCollectionsAggregationReqDto reqDto) {
        AddCollectionsAggregationRespDto retData = new AddCollectionsAggregationRespDto();
        Map<String, StandardDataDto> index = new HashMap<>();
        for (StandardDataDto one : reqDto.getStandardDataList()) {
            index.put(one.getEntityId(), one);
        }
        for (SameLevelEvaObjTargetCycleDto one : reqDto.getSameLevelEvaObjTargetCycleList()) {
            StandardDataDto elm = new StandardDataDto();
            elm.setEntityId(one.getEvaObjTargetCycleId());
            elm.setWeightValue(one.getWeightValue());
            elm.setWeightScore(one.getWeightScore());
            if (index.containsKey(one.getEvaObjTargetCycleId())) {
                elm.setDataResult(index.get(one.getEvaObjTargetCycleId()).getDataResult());
                elm.setDataResultId(index.get(one.getEvaObjTargetCycleId()).getDataResultId());
                retData.getStandardDataList().add(elm);
            }
        }
        return retData;
    }

    /**
     * code:getPublicFieldCache
     * name:M4-执行接收字段（特殊方法）
     * desc:undefined
     * gen by moon at 8/28/2022, 4:09:01 PM
     **/
    @Trace(operationName = "M4-执行接收字段（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementReceiveFieldRespDto implementReceiveField(ImplementReceiveFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementReceiveFieldRespDto.class);
    }

    /**
     * code:columnToRow
     * name:M4-获取标准数据列转单行（特殊方法）
     * desc:undefined
     * gen by moon at 9/6/2022, 10:19:21 PM
     **/
    @Trace(operationName = "M4-获取标准数据列转单行（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public AddStandardDataColumnToRowRespDto addStandardDataColumnToRow(AddStandardDataColumnToRowReqDto reqDto) {
        AddStandardDataColumnToRowRespDto retData = new AddStandardDataColumnToRowRespDto();
        Field[] declaredFields = AddStandardDataColumnToRowRespDto.class.getDeclaredFields();
        Map<String, Field> tarGetValueIndex = new HashMap<>();
        for (Field field : declaredFields) {
            tarGetValueIndex.put(field.getName(), field);
        }
        for (StandardDataDto oneItem : reqDto.getStandardDataList()) {
            retData.setEntityId(oneItem.getEntityId());
            Field tarGetField = tarGetValueIndex.get(CommonFunctionHelper.underlineToHump(oneItem.getDataType()));
            if (tarGetField != null) {
                try {
                    tarGetField.setAccessible(true);
                    tarGetField.set(retData, oneItem.getDataResult());
                    Field unitField = tarGetValueIndex.get(CommonFunctionHelper.underlineToHump(oneItem.getDataType()) + "Unit");
                    if (unitField != null) {
                        unitField.setAccessible(true);
                        unitField.set(retData, oneItem.getUnit());
                    }
//                     String tmp = CommonFunctionHelper.underlineToHump(oneItem.getDataType());
//                     String first = tmp.substring(0,1).toUpperCase(Locale.ROOT);
//                     String after = tmp.substring(1);
//                     tmp = first + after;
//                     Field parentField = tarGetValueIndex.get("parent"+tmp);
//                     if(parentField!=null){
//                         parentField.setAccessible(true);
//                         parentField.set(retData, oneItem.getParentDataResult());
//                     }
                    //新加逻辑
                    if (oneItem.getDataType().equals("PROGRESS_VALUE")) {
                        retData.setFromLastProgressValue(oneItem.getChangeFromLastValue());
                        retData.setFromLastProgressValueUnit(oneItem.getUnit());
                        retData.setProgressFromLastRatio(oneItem.getChangeFromLastRatio());
                        retData.setProgressFromLastRatioUnit("%");
                    } else if (oneItem.getDataType().equals("ACTUAL_VALUE")) {
                        retData.setChangeFromLastTarget(oneItem.getChangeFromLastValue());
                        retData.setChangeFromLastTargetUnit(oneItem.getUnit());
                        retData.setFromLastTargetLiftRatio(oneItem.getChangeFromLastRatio());
                        retData.setFromLastTargetLiftRatioUnit("%");
                    } else if (oneItem.getDataType().equals("REACH")) {
                        retData.setFromLastReach(oneItem.getChangeFromLastValue());
                        retData.setFromLastReachUnit(oneItem.getUnit());
                        retData.setActualFromLastTimeRatio(oneItem.getChangeFromLastRatio());
                        retData.setActualFromLastTimeRatioUnit("%");
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

        }
        return retData;
    }

    /**
     * code:lastestOfmultiPrm
     * name:M4-01-01查询数据类型过滤后标准数据列表
     * desc:undefined
     * gen by moon at 9/9/2022, 12:01:56 AM
     **/
    @Trace(operationName = "M4-01-01查询数据类型过滤后标准数据列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryDataTypeFilStandardDataListRespDto queryDataTypeFilStandardDataList(QueryDataTypeFilStandardDataListReqDto reqDto) {

        QueryDataTypeFilStandardDataListRespDto retData = new QueryDataTypeFilStandardDataListRespDto();
        Map<String, Map<String, StandardDataDto>> indexInfo = new HashMap<>();
        for (StandardDataDto oneData : reqDto.getStandardDataList()) {
            if (!indexInfo.containsKey(oneData.getEntityId() + oneData.getFromContentEntityId())) {
                Map<String, StandardDataDto> standInfoOfdataType = new HashMap<>();
                standInfoOfdataType.put(oneData.getDataType(), oneData);
                indexInfo.put(oneData.getEntityId() + oneData.getFromContentEntityId(), standInfoOfdataType);
            } else {
                Map<String, StandardDataDto> standInfoOfdataType = indexInfo.get(oneData.getEntityId() + oneData.getFromContentEntityId());
                if (!standInfoOfdataType.containsKey(oneData.getDataType())) {
                    standInfoOfdataType.put(oneData.getDataType(), oneData);
                } else {
                    StandardDataDto exisitData = standInfoOfdataType.get(oneData.getDataType());
                    if (exisitData.getCreateTime().compareTo(oneData.getCreateTime()) < 0) {
                        standInfoOfdataType.put(oneData.getDataType(), oneData);
                    }
                }
            }
        }

        for (Map.Entry<String, Map<String, StandardDataDto>> oneData : indexInfo.entrySet()) {
            retData.getStandardDataList().addAll(oneData.getValue().values().stream().map(p -> BeanUtil.toBean(p, StandardDataDto.class)).collect(Collectors.toList()));
        }


        return retData;
    }

    /**
     * code:columnToRow
     * name:M4-获取标准数据列转单行（上一执行周期）
     * desc:undefined
     * gen by moon at 1/4/2023, 11:44:13 PM
     *
     * @return
     */
    @Trace(operationName = "M4-获取标准数据列转单行（上一执行周期）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainStandardDataColumnToRowRespDto obtainStandardDataColumnToRow(ObtainStandardDataColumnToRowReqDto reqDto) {
        ObtainStandardDataColumnToRowRespDto retData = new ObtainStandardDataColumnToRowRespDto();
        Field[] declaredFields = ObtainStandardDataColumnToRowRespDto.class.getDeclaredFields();
        Map<String, Field> tarGetValueIndex = new HashMap<>();
        for (Field field : declaredFields) {
            tarGetValueIndex.put(field.getName(), field);
        }
        for (StandardDataDto oneItem : reqDto.getStandardDataList()) {
//             retData.setEntityId(oneItem.getEntityId());
            Field tarGetField = tarGetValueIndex.get(CommonFunctionHelper.underlineToHump(oneItem.getDataType()));
            if (tarGetField != null) {
                try {
                    tarGetField.setAccessible(true);
                    if (Objects.equals(tarGetField.getType(), String.class)) {
                        tarGetField.set(retData, String.valueOf(oneItem.getDataResult()));
                    } else {
                        tarGetField.set(retData, oneItem.getDataResult());
                    }

                    Field unitField = tarGetValueIndex.get(CommonFunctionHelper.underlineToHump(oneItem.getDataType()) + "Unit");
                    if (unitField != null) {
                        unitField.setAccessible(true);
                        if (unitField.getType().equals(Double.class)) {
                            unitField.set(retData, Double.valueOf(oneItem.getUnit()));
                        } else {
                            unitField.set(retData, oneItem.getUnit());
                        }

                    }
//                     String tmp = CommonFunctionHelper.underlineToHump(oneItem.getDataType());
//                     String first = tmp.substring(0,1).toUpperCase(Locale.ROOT);
//                     String after = tmp.substring(1);
//                     tmp = first + after;
//                     Field parentField = tarGetValueIndex.get("parent"+tmp);
//                     if(parentField!=null){
//                         parentField.setAccessible(true);
//                         if(parentField.getType().equals(Double.class)){
//                             parentField.set(retData, Double.valueOf(oneItem.getParentDataResult()));
//                         }else if(parentField.getType().equals(String.class)){
//                             parentField.set(retData, String.valueOf(oneItem.getParentDataResult()));
//                         }else{
//                             parentField.set(retData, oneItem.getParentDataResult());
//                         }
//
//                     }
                    //新加逻辑
                    if (oneItem.getDataType().equals("PROGRESS_VALUE")) {
                        retData.setFromLastProgressValue(oneItem.getChangeFromLastValue());
                        retData.setFromLastProgressValueUnit(oneItem.getUnit());
                        retData.setProgressFromLastRatio(oneItem.getChangeFromLastRatio());
                        retData.setProgressFromLastRatioUnit("%");
                    } else if (oneItem.getDataType().equals("ACTUAL_VALUE")) {
                        retData.setChangeFromLastTarget(oneItem.getChangeFromLastValue());
                        retData.setChangeFromLastTargetUnit(oneItem.getUnit());
                        retData.setFromLastTargetLiftRatio(oneItem.getChangeFromLastRatio());
                        retData.setFromLastTargetLiftRatioUnit("%");
                    } else if (oneItem.getDataType().equals("REACH")) {
                        retData.setFromLastReach(oneItem.getChangeFromLastValue());
                        retData.setFromLastReachUnit(oneItem.getUnit());
                        retData.setActualFromLastTimeRatio(oneItem.getChangeFromLastRatio());
                        retData.setActualFromLastTimeRatioUnit("%");
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

        }
        return retData;
    }

    /**
     * code:newestOfMultipleByPkeyAndTypeCode
     * name:M4-执行数据集主键加类型取最新（特殊方法）
     * desc:undefined
     * gen by moon at 1/11/2023, 9:10:26 PM
     **/
    @Trace(operationName = "M4-执行数据集主键加类型取最新（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementNewestOfMultipleByPkeyAndTypeCodeRespDto implementNewestOfMultipleByPkeyAndTypeCode(ImplementNewestOfMultipleByPkeyAndTypeCodeReqDto reqDto) {
        ImplementNewestOfMultipleByPkeyAndTypeCodeRespDto retData = new ImplementNewestOfMultipleByPkeyAndTypeCodeRespDto();
        Map<String, List<StandardDataDto>> index = new HashMap<>();
        for (StandardDataDto one : reqDto.getStandardDataList()) {
            String key = one.getDataType() + one.getBelongToContentId();
            if (index.containsKey(key)) {
                index.get(key).add(one);
            } else {
                List<StandardDataDto> subList = new ArrayList<>();
                subList.add(one);
                index.put(key, subList);
            }
        }

        for (List<StandardDataDto> one : index.values()) {
            StandardDataDto target = null;
            for (StandardDataDto subOne : one) {
                if (target == null) {
                    target = subOne;
                    continue;
                }
                if (subOne.getCreateTime().compareTo(target.getCreateTime()) > 0) {
                    target = subOne;
                }
            }
            retData.getStandardDataList().add(target);
        }
        return retData;
    }

    /**
     * code:findTargetDataByIntervalData
     * name:M4-执行根据数据区间获取目标数据记录（特殊方法）
     * desc:undefined
     * gen by moon at 1/12/2023, 4:03:14 AM
     **/
    @Trace(operationName = "M4-执行根据数据区间获取目标数据记录（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementFindTargetDataByIntervalDataRespDto implementFindTargetDataByIntervalData(ImplementFindTargetDataByIntervalDataReqDto reqDto) {
        ImplementFindTargetDataByIntervalDataRespDto retData = new ImplementFindTargetDataByIntervalDataRespDto();
        FindTargetDataByIntervalDataDto target = null;
        for (FindTargetDataByIntervalDataDto one : reqDto.getFindTargetDataByIntervalDataList()) {
            if (one.getComFloatField() == null || reqDto.getComFloatField() == null) {
                continue;
            }
            if (reqDto.getComTxtField().equals("FIND_BIG_DATA")) {
                if (one.getComFloatField().doubleValue() >= reqDto.getComFloatField().doubleValue()) {
                    if (target != null && target.getComFloatField().doubleValue() <= one.getComFloatField().doubleValue()) {
                        continue;
                    }
                    target = one;
                } else {
                    continue;
                }
            }
        }
        if (target == null) {
            return retData;
        }
        retData.setComTxtField(target.getComTxtField());
        retData.setComFloatField(target.getComFloatField());
        retData.setCommPrimaryKey(target.getCommPrimaryKey());
        return retData;
    }

    /**
     * code:findTargetDataFromListIntervalData
     * name:M4-执行根据数据区间获取目标数据记录-数据集（特殊方法）
     * desc:undefined
     * gen by moon at 1/13/2023, 3:28:40 AM
     **/
    @Trace(operationName = "M4-执行根据数据区间获取目标数据记录-数据集（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementFindTargetDataFromListyIntervalDataRespDto implementFindTargetDataFromListyIntervalData(ImplementFindTargetDataFromListyIntervalDataReqDto reqDto) {
        ImplementFindTargetDataFromListyIntervalDataRespDto retData = new ImplementFindTargetDataFromListyIntervalDataRespDto();
        for (DataSetsListOneDto one : reqDto.getDataSetsListOne()) {
            DataSetsListOneDto target = null;
            for (DataSetsListTwoDto sub : reqDto.getDataSetsListTwo()) {
                if (one.getComFloatField() == null || sub.getComFloatField() == null) {
                    continue;
                }
                if (one.getComFloatField().doubleValue() >= sub.getComFloatField().doubleValue()) {
                    if (target != null && target.getComFloatField().doubleValue() <= one.getComFloatField().doubleValue()) {
                        continue;
                    }
                    if (target == null) {
                        target = new DataSetsListOneDto();
                    }
                    target.setCommPrimaryKey(one.getCommPrimaryKey());
                    target.setComFloatField(sub.getComFloatField());
                    target.setComTxtField(sub.getComTxtField());
                } else {
                    continue;
                }
            }
            if (target != null) {
                retData.getDataSetsListOne().add(target);
            }
        }
        return retData;
    }

    /**
     * code:receptionService
     * name:M3约定是否向上计算为“1”
     * desc:undefined
     * gen by moon at 3/15/2023, 3:47:53 AM
     **/
    @Trace(operationName = "M3约定是否向上计算为“1”")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAccessParameterBizFieldRespDto implementAccessParameterBizField(ImplementAccessParameterBizFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementAccessParameterBizFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M4-出参下级OKR属性
     * desc:undefined
     * gen by moon at 6/19/2023, 8:13:16 AM
     **/
    @Trace(operationName = "M4-出参下级OKR属性")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public AddAcceptFieldRespDto addAcceptField(AddAcceptFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, AddAcceptFieldRespDto.class);
    }

    /**
     * code:getPublicFieldCache
     * name:M4-获取内存中业务字段（特殊方法）
     * desc:undefined
     * gen by moon at 6/24/2023, 8:26:58 AM
     **/
    @Trace(operationName = "M4-获取内存中业务字段（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainGetPublicFieldCacheRespDto obtainGetPublicFieldCache(ObtainGetPublicFieldCacheReqDto reqDto) {
        ObtainGetPublicFieldCacheRespDto retData = new ObtainGetPublicFieldCacheRespDto();
        String businessData = CommonFunctionHelper.getBaseInfoFromLocal().getBusinessData();
        JSONObject localDataJson = JSONObject.parseObject(businessData);
        Field[] declaredFields = retData.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            try {
                if (ObjectUtils.isNotEmpty(localDataJson.get(field.getName()))) {
                    if (field.getType().equals(Long.class)) {
                        field.set(retData, Long.valueOf((String) localDataJson.get(field.getName())));
                    } else if (field.getType().equals(Date.class)) {
                        Date tmpDate = new Date(Long.valueOf(localDataJson.getString(field.getName())));
                        field.set(retData, tmpDate);
                    } else {
                        field.set(retData, localDataJson.get(field.getName()));
                    }

                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return retData;
    }

    /**
     * code:publicFieldCache
     * name:M4-执行业务应用业务字段推送内存（特殊方法）
     * desc:undefined
     * gen by moon at 6/24/2023, 8:28:24 AM
     **/
    @Trace(operationName = "M4-执行业务应用业务字段推送内存（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementBizBusinessFieldPushMemoryRespDto implementBizBusinessFieldPushMemory(ImplementBizBusinessFieldPushMemoryReqDto reqDto) {
        ImplementBizBusinessFieldPushMemoryRespDto retData = new ImplementBizBusinessFieldPushMemoryRespDto();
        JSONObject jo = new JSONObject();
        Field[] declaredFields = reqDto.getClass().getDeclaredFields();
        try {
            for (Field field : declaredFields) {
                field.setAccessible(true);
                Object value = field.get(reqDto);
                if (value != null) {
                    jo.put(field.getName(), value);
                }
            }
        } catch (Exception e) {

        }
        CommonFunctionHelper.getBaseInfoFromLocal().setBusinessData(jo.toJSONString());
        return retData;
    }

    /**
     * code:multiColumnToMultiRow
     * name:M4-获取标准数据列转多行By周期阶段资料ID（特殊方法）
     * desc:undefined
     * gen by moon at 7/6/2023, 10:39:30 PM
     **/
    @Trace(operationName = "M4-获取标准数据列转多行By周期阶段资料ID（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainStandardDataColumnToMultiRowByStageDataRespDto obtainStandardDataColumnToMultiRowByStageData(ObtainStandardDataColumnToMultiRowByStageDataReqDto reqDto) {
        ObtainStandardDataColumnToMultiRowByStageDataRespDto retData = new ObtainStandardDataColumnToMultiRowByStageDataRespDto();
        Field[] declaredFields = StandardDataDto.class.getDeclaredFields();
        Map<String, Field> tarGetValueIndex = new HashMap<>();
        for (Field field : declaredFields) {
            tarGetValueIndex.put(field.getName(), field);
        }
        Map<String, StandardDataDto> arData = new HashMap<>();
        for (StandardDataDto oneItem : reqDto.getStandardDataList()) {
            if (!arData.containsKey(oneItem.getFromContentEntityId())) {
                StandardDataDto elm = new StandardDataDto();
                arData.put(oneItem.getFromContentEntityId(), elm);
            }
            StandardDataDto elm = arData.get(oneItem.getFromContentEntityId());

            String key = CommonFunctionHelper.underlineToHump(oneItem.getDataType());
            Field valueField = tarGetValueIndex.get(key);
            elm.setEntityId(oneItem.getEntityId());
            elm.setFromContentEntityId(oneItem.getFromContentEntityId());
            elm.setWorkCycleUpdateSlice(oneItem.getWorkCycleUpdateSlice());


            try {
                if (valueField != null) {
                    valueField.setAccessible(true);
                    if (Objects.equals(valueField.getType(), String.class)) {
                        valueField.set(elm, String.valueOf(oneItem.getDataResult()));
                    } else {
                        valueField.set(elm, oneItem.getDataResult());
                    }

                    Field unitField = tarGetValueIndex.get(key + "Unit");
                    if (unitField != null) {
                        unitField.setAccessible(true);
                        unitField.set(elm, oneItem.getUnit());
                    }
                    //新加逻辑
                    if (oneItem.getDataType().equals("PROGRESS_VALUE")) {
                        elm.setFromLastProgressValue(oneItem.getChangeFromLastValue());
                        elm.setFromLastProgressValueUnit(oneItem.getUnit());
                        elm.setProgressFromLastRatio(oneItem.getChangeFromLastRatio());
                        elm.setProgressFromLastRatioUnit("%");
                    } else if (oneItem.getDataType().equals("ACTUAL_VALUE")) {
                        elm.setChangeFromLastTarget(oneItem.getChangeFromLastValue());
                        elm.setChangeFromLastTargetUnit(oneItem.getUnit());
                        elm.setFromLastTargetLiftRatio(oneItem.getChangeFromLastRatio());
                        elm.setFromLastTargetLiftRatioUnit("%");
                    } else if (oneItem.getDataType().equals("REACH")) {
                        elm.setFromLastReach(oneItem.getChangeFromLastValue());
                        elm.setFromLastReachUnit(oneItem.getUnit());
                        elm.setActualFromLastTimeRatio(oneItem.getChangeFromLastRatio());
                        elm.setActualFromLastTimeRatioUnit("%");
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        retData.setStandardDataList(arData.values().stream().map(p -> BeanUtil.toBean(p, StandardDataDto.class)).collect(Collectors.toList()));

//         if(retData.getStandardDataList()!=null && retData.getStandardDataList().size()==1){
//             StandardDataDto firstData = retData.getStandardDataList().get(0);
//             BeanUtil.copyProperties(firstData,retData);
//         }
        return retData;
    }

    /**
     * code:multiColumnToMultiRow
     * name:M4-获取标准数据列转多行By工作周期更新切片
     * desc:undefined
     * gen by moon at 7/6/2023, 10:39:58 PM
     **/
    @Trace(operationName = "M4-获取标准数据列转多行By工作周期更新切片")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainStandardDataColumnToMultiRowByWorkCycleUpdateSliceRespDto obtainStandardDataColumnToMultiRowByWorkCycleUpdateSlice(ObtainStandardDataColumnToMultiRowByWorkCycleUpdateSliceReqDto reqDto) {
        ObtainStandardDataColumnToMultiRowByWorkCycleUpdateSliceRespDto retData = new ObtainStandardDataColumnToMultiRowByWorkCycleUpdateSliceRespDto();
        Field[] declaredFields = StandardDataDto.class.getDeclaredFields();
        Map<String, Field> tarGetValueIndex = new HashMap<>();
        for (Field field : declaredFields) {
            tarGetValueIndex.put(field.getName(), field);
        }
        Map<String, StandardDataDto> arData = new HashMap<>();
        for (StandardDataDto oneItem : reqDto.getStandardDataList()) {
            if (!arData.containsKey(oneItem.getWorkCycleUpdateSlice())) {
                StandardDataDto elm = new StandardDataDto();
                arData.put(oneItem.getWorkCycleUpdateSlice(), elm);
            }
            StandardDataDto elm = arData.get(oneItem.getWorkCycleUpdateSlice());

            String key = CommonFunctionHelper.underlineToHump(oneItem.getDataType());
            Field valueField = tarGetValueIndex.get(key);
            elm.setEntityId(oneItem.getEntityId());
            elm.setFromContentEntityId(oneItem.getFromContentEntityId());
            elm.setWorkCycleUpdateSlice(oneItem.getWorkCycleUpdateSlice());


            try {
                if (valueField != null) {
                    valueField.setAccessible(true);
                    if (Objects.equals(valueField.getType(), String.class)) {
                        valueField.set(elm, String.valueOf(oneItem.getDataResult()));
                    } else {
                        valueField.set(elm, oneItem.getDataResult());
                    }

                    Field unitField = tarGetValueIndex.get(key + "Unit");
                    if (unitField != null) {
                        unitField.setAccessible(true);
                        unitField.set(elm, oneItem.getUnit());
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        retData.setStandardDataList(arData.values().stream().map(p -> BeanUtil.toBean(p, StandardDataDto.class)).collect(Collectors.toList()));

        if (retData.getStandardDataList() != null && retData.getStandardDataList().size() == 1) {
            StandardDataDto firstData = retData.getStandardDataList().get(0);
            BeanUtil.copyProperties(firstData, retData);
        }
        return retData;
    }

    /**
     * code:publicFieldCache
     * name:M4-执行分离提取更新周期最新标准数据测试（特殊方法）
     * desc:undefined
     * gen by moon at 6/24/2023, 8:28:24 AM
     **/
    @Trace(operationName = "M4-执行分离提取更新周期最新标准数据测试（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementSeparationAndExtractionUpdateCycleDataTestRespDto implementSeparationAndExtractionUpdateCycleDataTest(ImplementSeparationAndExtractionUpdateCycleDataTestReqDto reqDto) {
        ImplementSeparationAndExtractionUpdateCycleDataTestRespDto retData = new ImplementSeparationAndExtractionUpdateCycleDataTestRespDto();
        Map<String, List<StandardDataDto>> groupIndex = new HashMap<>();
        for (StandardDataDto one : reqDto.getStandardDataDtoList()) {
            if (!groupIndex.containsKey(one.getPeriodId())) {
                List<StandardDataDto> singleGroupList = new ArrayList<>();
                singleGroupList.add(one);
                groupIndex.put(one.getPeriodId(), singleGroupList);
            } else {
                groupIndex.get(one.getPeriodId()).add(one);
            }
        }
        List<StandardDataDto> oneList = new ArrayList<>();
        boolean bFlag = false;
        String filterId = "";
        for (Map.Entry<String, List<StandardDataDto>> oneEntry : groupIndex.entrySet()) {
            for (StandardDataDto one : oneEntry.getValue()) {
                if (one.getUpdateCycleId() != null
                        && one.getUpdateCycleId().equals(reqDto.getUpdateCycleId())
                        && one.getIsUpdateCycleNewData() != null
                        && one.getIsUpdateCycleNewData().equals("TRUE")) {
                    oneList.add(one);
                    bFlag = true;
                    filterId = oneEntry.getKey();
                    break;
                }
            }
            if (bFlag) {
                break;
            }
        }

        for (Map.Entry<String, List<StandardDataDto>> oneEntry : groupIndex.entrySet()) {
            if (oneEntry.getKey().equals(filterId)) {
                continue;
            }
            for (StandardDataDto one : oneEntry.getValue()) {
                if (one.getIsNewData() != null
                        && one.getIsNewData().equals("TRUE")) {
                    oneList.add(one);
                }
            }
        }
        Collections.sort(oneList, new Comparator<StandardDataDto>() {
            @Override
            public int compare(StandardDataDto p1, StandardDataDto p2) {
                if (p1.getReleaseTime().compareTo(p2.getReleaseTime()) < 0) {
                    return 1;
                }
                return 0;
            }
        });
        retData.setStandardDataDtoList(oneList);
        return retData;
    }

    /**
     * code:separationAndExtractionUpdateCycleData
     * name:M4-执行分离提取更新周期最新标准数据
     * desc:undefined
     * gen by moon at 7/14/2023, 5:59:17 PM
     **/
    @Trace(operationName = "M4-执行分离提取更新周期最新标准数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementSeparationAndExtractionUpdateCycleDataRespDto implementSeparationAndExtractionUpdateCycleData(ImplementSeparationAndExtractionUpdateCycleDataReqDto reqDto) {
        ImplementSeparationAndExtractionUpdateCycleDataRespDto retData = new ImplementSeparationAndExtractionUpdateCycleDataRespDto();
        Map<String, List<StandardDataDto>> groupIndex = new HashMap<>();
        for (StandardDataDto one : reqDto.getStandardDataList()) {
            if (!groupIndex.containsKey(one.getPeriodId())) {
                List<StandardDataDto> singleGroupList = new ArrayList<>();
                singleGroupList.add(one);
                groupIndex.put(one.getPeriodId(), singleGroupList);
            } else {
                groupIndex.get(one.getPeriodId()).add(one);
            }
        }
        List<StandardDataDto> oneList = new ArrayList<>();
        boolean bFlag = false;
        String filterId = "";
        for (Map.Entry<String, List<StandardDataDto>> oneEntry : groupIndex.entrySet()) {
            for (StandardDataDto one : oneEntry.getValue()) {
                if (one.getUpdateCycleId() != null
                        && one.getUpdateCycleId().equals(reqDto.getUpdateCycleId())
                        && one.getIsUpdateCycleNewData() != null
                        && one.getIsUpdateCycleNewData().equals("TRUE")) {
                    oneList.add(one);
                    bFlag = true;
                    filterId = oneEntry.getKey();
                    break;
                }
            }
            if (bFlag) {
                break;
            }
        }

        for (Map.Entry<String, List<StandardDataDto>> oneEntry : groupIndex.entrySet()) {
            if (oneEntry.getKey().equals(filterId)) {
                continue;
            }
            for (StandardDataDto one : oneEntry.getValue()) {
                if (one.getIsNewData() != null
                        && one.getIsNewData().equals("TRUE")) {
                    oneList.add(one);
                }
            }
        }
        Collections.sort(oneList, new Comparator<StandardDataDto>() {
            @Override
            public int compare(StandardDataDto p1, StandardDataDto p2) {
                if (p1.getReleaseTime().compareTo(p2.getReleaseTime()) < 0) {
                    return 1;
                }
                return 0;
            }
        });
        retData.setStandardDataList(oneList);
        return retData;
    }

    /**
     * code:receptionService
     * name:M4接收入参字段
     * desc:undefined
     * gen by moon at 3/8/2024, 3:43:45 AM
     **/
    @Trace(operationName = "M4接收入参字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementCountSecReceFieldRespDto implementCountSecReceField(ImplementCountSecReceFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementCountSecReceFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M4接收财务标准数据数据集（用于循环发牌）
     * desc:undefined
     * gen by moon at 3/19/2024, 12:03:09 PM
     **/
    @Trace(operationName = "M4接收财务标准数据数据集（用于循环发牌）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementNineReceivingFieldRespDto implementNineReceivingField(ImplementNineReceivingFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementNineReceivingFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M4执行大屏接收字段
     * desc:undefined
     * gen by moon at 4/2/2024, 6:27:12 PM
     **/
    @Trace(operationName = "M4执行大屏接收字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementDashboardReceiveFieldRespDto implementDashboardReceiveField(ImplementDashboardReceiveFieldReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementDashboardReceiveFieldRespDto.class);
    }

    /**
     * code:receptionService
     * name:M4接收财务数据出参
     * desc:undefined
     * gen by moon at 7/5/2024, 7:31:34 PM
     **/
    @Trace(operationName = "M4接收财务数据出参")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementCalcReceivingField006RespDto implementCalcReceivingField006(ImplementCalcReceivingField006ReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementCalcReceivingField006RespDto.class);
    }

    /**
     * code:receptionService
     * name:M4接收入参字段
     * desc:undefined
     * gen by moon at 10/23/2024, 6:50:47 PM
     **/
    @Trace(operationName = "M4接收入参字段")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementCalcReceivingField001RespDto implementCalcReceivingField001(ImplementCalcReceivingField001ReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementCalcReceivingField001RespDto.class);

    }

    /**
     * code:receptionService
     * name:M4接收出参字段：实际值
     * desc:undefined
     * gen by moon at 11/12/2024, 6:42:49 PM
     **/
    @Trace(operationName = "M4接收出参字段：实际值")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementCalcReceivingField005RespDto implementCalcReceivingField005(ImplementCalcReceivingField005ReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ImplementCalcReceivingField005RespDto.class);
    }
    //手工接入方法
}
